define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'student/index' + location.search,
                    add_url: 'student/add',
                    edit_url: 'student/edit',
                    del_url: 'student/setclassid2zero',
                    multi_url: 'student/multi',
                    table: 'student',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: true,
                showExport: false,
                showToggle: false,
                showColumns: false,
                commonSearch: true,
                searchFormVisible: true,
                queryParams: function (params) {
                    //这里可以追加搜索条件
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    if($("#year_").val()){
                        filter.year_ = $("#year_").val();
                        op.admin_id = "=";
                    }

                    if($("#classes_id").val()){
                        filter.classes_id = $("#classes_id").val();
                        op.classes_id = "=";
                    }

                    //if($("#is_reported").val()){
                        filter.is_reported = $("#is_reported").val();
                        op.is_reported = "=";
                    //}

                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: '', title: __('序号'),operate:false,table:table,formatter: Table.api.formatter.natureorder},
                        {field: 'sid', title: __('Sid'),operate:'like'},
                        {field: 'nickname', title: __('Nickname'),operate:'like'},
                        {field: 'classes_name', title: __('Classes_name'),operate:'like'},
                        {field: 'classes_id', title: __('Classes_id'),operate:false,visible:false},

                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('推送'),
                            table: table,
                            events: Table.api.events.operate,
                            operate:false,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: function (row) {
                                        if(row.status == '1'){
                                            return '停用';
                                        }else if(row.status == '0'){
                                            return '恢复';
                                        }
                                    },
                                    title: __('停用或恢复'),
                                    classname: function(row) {
                                        if(row.status == '1'){
                                            return 'btn btn-xs btn-success btn-magic btn-ajax';
                                        }else if(row.status == '0'){
                                            return 'btn btn-xs btn-danger btn-magic btn-ajax';
                                        }
                                    },
                                    icon: 'fa fa-magic',
                                    url: 'student/setclassid2zero',
                                    confirm: '确认操作？',
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh');
                                        // Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        ana: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'student/ana' + location.search,
                    add_url: 'student/add',
                    edit_url: 'student/edit',
                    del_url: 'student/del',
                    multi_url: 'student/multi',
                    table: 'student',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                showExport: true,
                showToggle: false,
                showColumns: false,
                commonSearch: true,
                searchFormVisible: true,
                searchFormTemplate: 'searchformtpl',
                queryParams: function (params) {
                    //这里可以追加搜索条件
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    if($("#year_").val()){
                        filter.year_ = $("#year_").val();
                        op.year_ = "=";
                    }

                    if($("#classes_id").val()){
                        filter.classes_id = $("#classes_id").val();
                        op.classes_id = "=";
                    }

                    //if($("#is_reported").val()){
                    filter.create_time = $("#create_time").val();
                    op.create_time = "between";
                    //}

                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
                columns: [
                    [
                        {field: '', title: __('序号'),operate:false,table:table,formatter: Table.api.formatter.natureorder},
                        {field: 'sid', title: __('Sid'),operate:false},
                        {field: 'nickname', title: __('Nickname'),operate:false},
                        {field: 'year_', title: __('Year_')},
                        {field: 'classes_name', title: __('Classes_name'),addClass: "selectpage", extend: "data-source='classes/index' data-field='name'"},
                        {field: 'classes_id', title: __('Classes_id'),operate:false,visible:false},
                        {field: '', title: __('Is_reported'),formatter: function (value, row, index) {
                                return "否";
                            }},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});